package com.obas.view.explorer {
   import com.ease.core.events.ViewOperationEvent;
   import com.ease.framework.CommandCompletionEvent;
   import com.obas.business.taskexecution.vo.TestPlanObject;
   import com.obas.component.tree.treeclass.Node;
   import com.obas.constants.TestingLibConstants;
   import com.obas.event.DataRefreshEvent;
   import com.obas.event.ItestOperationEvent;
   import com.obas.event.TestPlanOperationEvent;
   import com.obas.flex.common.util.ListDataUtil;
   import com.obas.flex.common.util.ResourceUtil;
   import com.obas.model.business.SystemFunction;
   import com.obas.model.business.SystemProject;
   import com.obas.model.business.SystemScenario;
   import com.obas.model.business.SystemUsecase;
   import com.obas.model.business.TestPlan;
   import com.obas.model.business.TestTask;
   import com.obas.model.business.TestTaskItem;
   import com.obas.model.business.TestcaseScript;
   import com.obas.project.view.ProjectNewView;
   import com.obas.view.menu.TestPlanMenu;
   import com.obas.view.testplan.wizard.TestPlanWizard;
   import com.ease.component.managers.ErrorManager;
   import com.ease.component.managers.ProgressManager;

   import flash.events.ContextMenuEvent;
   import flash.events.EventDispatcher;

   import mx.collections.ArrayCollection;
   import mx.events.ListEvent;
   import mx.managers.PopUpManager;

   [Event(name="{com.ease.core.events.ViewOperationEvent.REQUEST_ID}", type="com.ease.core.events.ViewOperationEvent")]
   [Event(name="{com.obas.event.TestPlanOperationEvent.REQUEST_ID}", type="com.obas.event.TestPlanOperationEvent")]
   public class ITestPlanExplorerViewMediator extends EventDispatcher {
      private var _pageId:String;
      private var listUtil:ListDataUtil = new ListDataUtil(keyFunction);

      public var _view:ITestPlanExplorerView;

      [InjectableView]
      public function get view():ITestPlanExplorerView {
         return _view;
      }

      public function set view(value:ITestPlanExplorerView):void {
         _view = value;
         init();
      }

      private function init():void {
         view.addEventListener(TestPlanMenu.CREATE_TEST_PLAN, onCreatePlan);
         view.addEventListener(TestPlanMenu.DELETE_TEST_PLAN, onDeletePlan);
         view.addEventListener(TestPlanMenu.RUN_TEST_PLAN, onRunPlan);
         view.addEventListener("refresh", onRefresh);

         view.navTree.addEventListener(ListEvent.ITEM_DOUBLE_CLICK, onDoubleClick);
         onRefresh();
      }

      private function onRefresh(e:* = null):void {
         ProgressManager.showProgress(view, ProgressManager.LOADING_SPINNER);
         dispatchEvent(new TestPlanOperationEvent(null, TestPlanOperationEvent.GET_TEST_PLAN));
      }

      private function keyFunction(item:*):String {
         return item.entity.id + "";
      }

      [ResponseHandler("{com.obas.event.TestPlanOperationEvent.RETURN_ID}")]
      public function onTestPlanOperationEvent(event:CommandCompletionEvent):void {
         ProgressManager.hideProgress(view);
         if (event.error) {
            ErrorManager.showErrorMessage(ResourceUtil.getErrorString("", event.error));
            return ;
         }

         var request:TestPlanOperationEvent = event.request as TestPlanOperationEvent;
         if (request.actionType == TestPlanOperationEvent.RUN_TEST_PLAN) {

         }
         else {
            var results:ArrayCollection = event.result as ArrayCollection;
            view.treeNodes = new ArrayCollection();
            for each (var plan:TestPlan in results) {
               var childNode:Node = view.navTree.nodeFactory.createNode(plan);
               childNode.type = TestingLibConstants.ITEMTYPE_TEST_PLAN;
               view.treeNodes.addItem(childNode);
            }
         }
      }

      private function onCreatePlan(e:*):void {
         var dialog:TestPlanWizard = new TestPlanWizard();
         dialog.title = "创建测试计划";
         PopUpManager.addPopUp(dialog, view.root, true);
         PopUpManager.centerPopUp(dialog);
      }

      private function onDeletePlan(e:*):void {
      }

      private function onRunPlan(e:*):void {
         _pageId = "IE" + new Date().time;
         dispatchEvent(new TestPlanOperationEvent([_pageId, view.navTree.selectedItem.entity.id], TestPlanOperationEvent.RUN_TEST_PLAN));
      }


      private function onDoubleClick(e:*):void {
         var selectedNode:Node = view.navTree.selectedItem as Node;
         if (selectedNode.type == TestingLibConstants.ITEMTYPE_TEST_PLAN) {
            var param:Object = new Object();
            param.targetView = "commonView";
            param.className = "com.obas.view.testplan.TestPlanView";
            param.createNew = "true";
            param.autoOpen = "true";
            param.entity = selectedNode.entity;
            dispatchEvent(new ViewOperationEvent([param.targetView], param));
         } else {
            var paramOther:Object = new Object();
            paramOther.targetView = "commonView";
            paramOther.className = "com.obas.view.EntityView";
            param.createNew = "true";
            param.autoOpen = "true";
            paramOther.model = selectedNode.entity;
            dispatchEvent(new ViewOperationEvent([paramOther.targetView], paramOther));
         }
      }

      [EventHandler("{com.obas.event.DataRefreshEvent.REQUEST_ID}")]
      public function onDataRefreshEvent(event:DataRefreshEvent):void {
         switch (event.actionType) {
            case DataRefreshEvent.TYPE_DELETE :
               deleteItems(event.spec.data);
               break;
            case DataRefreshEvent.TYPE_ADD_UPDATE :
               var parentNode:Node = event.spec.context;
               var datas:ArrayCollection = event.spec.data;
               if (datas && datas.length > 0) {
                  var item:* = datas.getItemAt(0);
                  var nodeType:String = getNodeType(item);
                  if(nodeType == "") {
                     return;
                  }
                  var childNode:Node = view.navTree.nodeFactory.createNode(item);
                  childNode.type = nodeType;
                  if (nodeType == TestingLibConstants.ITEMTYPE_TASK_ITEM) {
                     childNode.children = null;
                  }
                  var nodes:Array = [childNode];
                 listUtil.addOrUpdateItems(view.treeNodes.source, nodes, true);
                 view.treeNodes.refresh();
                 view.navTree.invalidateDisplayList();
               }

               break;
         }
      }

      private function deleteItems(items:ArrayCollection):void {
      }

      public function getNodeType(item:*):String {
         if (item is TestPlan) {
            return TestingLibConstants.ITEMTYPE_TEST_PLAN;
         }
         else if (item is TestTask) {
            return TestingLibConstants.ITEMTYPE_TEST_TASK;
         }
         else if (item is TestTaskItem) {
            return TestingLibConstants.ITEMTYPE_TASK_ITEM;
         }
         return "";
      }

   }
}